<template>
  <ContentWrap class="chem-record-wrap">
    <div class="header-container-consumable">
      <!-- Title Centered -->
      <h2 class="centered-title-equipment">样品接收登记记录</h2>
      <!-- Buttons Right Aligned -->
      <div class="header-button-container-equipment">
        <el-button type="primary" @click="onSyncHeader">同步</el-button>
        <el-button type="success" @click="onPreviewHeader" style="margin-left: 8px;">预览</el-button>
      </div>
    </div>
    <!-- 头部信息表单 -->
    <el-form :model="form" class="chem-form" label-width="100px">
      <el-row :gutter="18" align="middle">
        <el-col :span="6">
          <el-form-item label="检测类别">
            <el-input v-model="form.type" placeholder="请输入检测类别" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采样日期">
            <el-date-picker
              v-model="form.sampleDate"
              placeholder="选择日期"
              style="width: 100%"
              type="date"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="样品总数">
            <el-input v-model="form.total" placeholder="请输入样品总数" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 表格 -->
    <el-table :data="form.table" border class="chem-table" stripe table-layout="fixed">
      <el-table-column type="index" align="center" label="序号"  /> >
      <el-table-column align="center" label="空气收集器" prop="airCollector"  >
        <template #default="{ row }">
          <el-input v-model="row.airCollector" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="测量编号" prop="sampleNo"  >
        <template #default="{ row }">
          <el-input v-model="row.sampleNo" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="采样体积(L)" prop="samplingVolume">
        <template #default="{ row }">
          <el-input v-model="row.samplingVolume" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="检测项目" prop="inspectionItems" >
        <template #default="{ row }">
          <el-input v-model="row.inspectionItems" placeholder="" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="样品状态" prop="sampleStatus">
        <template #default="{ row }">
          <el-input v-model="row.sampleStatus" placeholder="" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="样品保存期限和保存条件" prop="preservationConditions">
        <template #default="{ row }">
          <el-input v-model="row.preservationConditions" placeholder="" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark">
        <template #default="{ row }">
          <el-input v-model="row.remark" placeholder="" />
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import ContentWrap from '@/components/ContentWrap/src/ContentWrap.vue'
import { ProjectApi } from '@/api/dk/project'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { merge } from 'lodash-es'
const route = useRoute()
const router = useRouter()
const form = ref({
  type: '定期检测',
  sampleDate: '',
  projectId: route.query.projectId,
  total: '',
  table: [
    {
      sampleNo: '',
      inspectionItems: '',
      airCollector: '',
      sampleStatus: '',
      samplingVolume: '1',
      preservationConditions: '',
      remark: ''
    }
  ]
})
const loading = ref(false)

const getDefaultSampleHandoverRecord = async () => {
  const projectId = route.query.projectId as string
  const result = await ProjectApi.getDefaultSampleHandoverRecord(Number(projectId))
  form.value = merge({}, form.value, result)
}
watch(
  form,
  (newValue, oldValue) => {
    console.log('form changed:', newValue);
    // 在这里执行你的逻辑
    ProjectApi.updateSampleHandoverRecord(form.value)
    if (!loading.value) {
      ElMessage.success('修改成功')
      loading.value = false
    }
  },
  { deep: true }  // 深度监听，因为 form 是嵌套对象
);


// onMounted(async () => {
//   loading.value = true
//   const projectId = route.query.projectId as string
//   const data = await ProjectApi.getSampleHandoverRecord(Number(projectId))
//   console.log('数据', JSON.stringify(data))
//   form.value = merge({}, form.value, data)
// })

// 头部按钮事件
const onSyncHeader = async () => {
  await getDefaultSampleHandoverRecord()

}
const onPreviewHeader = () => {
  // 跳转到现场调查页面，并携带项目信息
  const { projectId, projectNo } = route.query
  router.push({
    path: '/previewFile',
    query: {
      projectId: projectId,
      projectNo: projectNo,
      operationType: 9
    }
  })
}
</script>

<style lang="scss" scoped>

.header-container-consumable {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e5e6eb;
}
.centered-title-equipment {
  text-align: center; // Center the text
  flex-grow: 1; // Allow title to take available space to help centering
  margin: 0; // Remove default margin
  font-size: 24px; // Match Consumable title size
  font-weight: bold;
}

.chem-form {
  background: none;
  box-shadow: none;
  border-radius: 0;
  margin-bottom: 0;

  :deep(.el-input__inner) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  :deep(.el-form-item__label) {
    white-space: nowrap;
  }
}

.chem-btns {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  align-items: center;
  margin-top: 0;
}

.chem-table {
  margin-top: 18px;
  --el-table-border-radius: 0 !important;
  --el-table-header-bg-color: #f7f8fa;
  --el-table-border-color: #e5e6eb;
  box-shadow: none !important;
  border-radius: 0 !important;

  :deep(.el-table__header th) {
    background: #f7f8fa !important;
    font-weight: 600;
    font-size: 14px;
    border-radius: 0 !important;
    padding: 8px 0 !important;

    .cell {
      padding-left: 0px !important;
      padding-right: 0px !important;
    }
  }

  // 设置表格行高
  :deep(.el-table__body td) {
    border-radius: 0 !important;

    font-size: 14px;
    padding: 0 !important;
    // 列和单元格的边距
    .cell {
      padding: 0px 0px;
    }
  }

  // 去掉输入框边框
  :deep(.el-input__wrapper) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  // 针对下拉选择框的所有相关元素去掉边框
  :deep(.el-select__wrapper) {
    border: none !important;
    box-shadow: none !important;
  }
}

.input-with-unit {
  position: relative;
}

:deep(.el-input__inner) {
  text-align: center;
}

.unit-text {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #909399;
  font-size: 14px;
  pointer-events: none;
  z-index: 1;
}
</style>
